/*********************************************************************\
*
*                  (c) lkc
*                  All Rights Reserved. lkc Confidential
*
\*********************************************************************/
/*********************************************************************\
*   Title                      :
*
*   Module Description         :
*
*   Author                     : lkc
*
*   Created                    : 2018-7-3
*
\*********************************************************************/

#include "ProtocolPod613.h"
#ifndef POD_IS_205
#include <math.h>
#include "CamGainControl.h"
#include "ImageDetect.h"
#include "CamControl.h"
#include "DisplayControl.h"
#include "UtilityTH.h"
#include "ProtocolMCU.h"

ProtocolPod613 ProtocolPod613::m_Instance;

ProtocolPod613& ProtocolPod613::GetInstance()
{
    return m_Instance;
}

ProtocolPod613::ProtocolPod613()
{
}

ProtocolPod613::~ProtocolPod613()
{
}

int ProtocolPod613::CycleSendOnce()
{
    int frameLenght = 49;
    unsigned char frameToSendCycle[frameLenght];
    memset(frameToSendCycle,0,frameLenght);
    frameToSendCycle[0] = HEAD1;
    frameToSendCycle[1] = HEAD2_CYCLE;
    frameToSendCycle[2] = NORMAL_CYCLE;   //数据状态
    frameToSendCycle[3] = m_funcCodeSend;   //功能代号
    frameToSendCycle[4] = m_commandSend | m_resultSend;   //具体操作及结果
    frameToSendCycle[5] = m_dataSend;

    std::vector <cv::Point2f> points = ImageDetect::GetInstance().GetObjectPoints();
    //字节长度最多十个点
    unsigned int num = points.size();
    if(num > OBJECT_NUM_MAX)
    {
        num = OBJECT_NUM_MAX;
    }
    frameToSendCycle[6] = num;
    int i = 7;
    for(unsigned int j = 0; j < num; j++)
    {
        int xCode = static_cast<int>(points[j].x*10);
        int yCode = static_cast<int>(points[j].y*10);
        //x坐标，浮点数保留一位小数
       frameToSendCycle[i++] = xCode&0xFF;
       frameToSendCycle[i++] = (xCode>>8)&0xFF;
        //y坐标
       frameToSendCycle[i++] = yCode&0xFF;
       frameToSendCycle[i++] = (yCode>>8)&0xFF;
    }
    for( ; i < 47 ; i++)
    {
        frameToSendCycle[i] = 0;
    }
    frameToSendCycle[47] = CheckSum(frameToSendCycle+2,frameLenght-4);       //校验和
    frameToSendCycle[48] = 0xFB;       //帧尾
    int ret = Send(frameToSendCycle,frameLenght);
    if (ret < 0)
    {
        printf("ProtocolPod write data error!\n");
    }
    return ret;
}


void ProtocolPod613::ExtractData(unsigned char *data, int nLength)
{
    bufferTemp.insert(bufferTemp.end(),data,data+nLength);

    while( bufferTemp.size() >= FRAME_LENGTH_RECEIVE)
    {
        auto iter = bufferTemp.begin();
        if (*iter != HEAD1)
            {
                //如果数据开始不是头，则删除数据
                bufferTemp.erase(iter);
                continue;
            }
        if(*(iter+1) != HEAD2_EVENT)
        {
//            bufferTemp.erase(iter,iter+2);
            bufferTemp.erase(iter);
            continue;
        }
        if(*(iter+FRAME_LENGTH_RECEIVE-1) != END)
        {
            bufferTemp.erase(iter,iter+2);
//            bufferTemp.erase(iter);
            continue;
        }

        unsigned char sum = CheckSum(iter+2,3);
        if(*(iter+FRAME_LENGTH_RECEIVE-2) != sum)
        {
//            bufferTemp.erase(iter,iter+FRAME_LENGTH_RECEIVE);
//            bufferTemp.erase(iter);
            bufferTemp.erase(iter,iter+2);
            continue;
        }

        //到此，找到一帧完整的数据
//        printf("########sum %x\n",sum);
//        printf("ExtractData MCU %x, %x, %x, %x, %x, %x, %x\n",bufferTemp[0],bufferTemp[1],bufferTemp[2],bufferTemp[3],bufferTemp[4],bufferTemp[5],bufferTemp[6]);
        EventReceiveFrameParse();
        bufferTemp.erase(iter,iter+FRAME_LENGTH_RECEIVE);
    }
}

void ProtocolPod613::EventReceiveFrameParse()
{
    m_funcCodeReceive = bufferTemp[2];
//    printf("commandReceive: %x\n",m_commandReceive);
    m_commandReceive = bufferTemp[3];
    m_dataReceive = bufferTemp[4];
    switch (m_funcCodeReceive)
    {
    case 0xC0:
        SelfCheckParse();
        break;
    case 0xC1:
        GainParse();
        break;
    case 0xC2:
        FoucsParse();
        break;
    case 0xC3:
        ContrastParse();
        break;
    case 0xC4:
        OutModeParse();
        break;
    case 0xC5:
        WorkModeParse();
        break;
    default:
        break;
    }
}

void ProtocolPod613::SelfCheckParse()
{
    if(m_commandReceive == 0x00)//启动相机自检
    {
        ProtocolMCU::GetInstance().MotoSelfCheck(this);
    }
}

void ProtocolPod613::GainParse()
{
    switch(m_commandReceive)
    {
    case 0x01://增加增益
        CamGainControl::GetInstance().IncGain();
        break;
    case 0x02://减小增益
        CamGainControl::GetInstance().DecGain();
        break;
    case 0x03://读取当前增益
        break;
    case 0x10://增益自动调节
        CamGainControl::GetInstance().SetGainAutoMode(true);
        break;
    default:
        return;
    }
    ReportGain(m_commandReceive,true);
}

void ProtocolPod613::FoucsParse()
{
    switch(m_commandReceive)
    {
    case 0x01://增加焦距
        ProtocolMCU::GetInstance().IncFocus(this);
        break;
    case 0x02://减小焦距
        ProtocolMCU::GetInstance().DecFocus(this);
        break;
    case 0x03://读取当前焦距
        ReportFocus(m_commandReceive,true);
        break;
    case 0x10://设置焦距绝对值
        ProtocolMCU::GetInstance().SetFocus(m_dataReceive,this);
        break;
    default:
        break;
    }
}

void ProtocolPod613::ContrastParse()
{
    switch(m_commandReceive)
    {
    case 0x01://增加对比度
        IncContrast();
        break;
    case 0x02://减小对比度
        DecContrast();
        break;
    case 0x03://读取当前对比度
        break;
    default:
        return;
    }
    ReportContrast(m_commandReceive,true);
}

void ProtocolPod613::OutModeParse()
{
    switch(m_commandReceive)
    {
    case 0x01://原图输出模式
        SetOutMode(OutMode::ORIGINAL);
        break;
    case 0x02://增强输出模式
        SetOutMode(OutMode::ENHANCE);
        break;
    case 0x03://信息叠加模式
        SetOutMode(OutMode::SUPERPOSITION);
        break;
    case 0x10://获取当前图像输出模式
        break;
    case 0x20://隐藏十字中心线
        SetCrossMode(false);
        break;
    case 0x21://显示十字中心线
        SetCrossMode(true);
        break;
    default:
        return;
    }
    ReportOutMode(m_commandReceive,true);

}

void ProtocolPod613::WorkModeParse()
{
    switch(m_commandReceive)
    {
    case 0x01://工作模式
        CamControl::GetInstance().SetWorkMode(true);
        break;
    case 0x02://待机模式，切断CCD与调焦系统
        CamControl::GetInstance().SetWorkMode(false);
        break;
    default:
        return;
    }
    ReportWorkMode(m_commandReceive,true);
}

void ProtocolPod613::ReportStartSelfCheck(bool result)
{
    DefaultSend(0x00, result?RESULT_SUCCESS:RESULT_FAIL);
    m_funcCodeSend = 0xC0;
//    bool state = CamControl::GetInstance().GetSelfCheckState();
    m_dataSend = ProtocolMCU::GetInstance().GetSelfCheckState()?0x00:0x01;
//    SendFrame();
}

void ProtocolPod613::ReportGain(unsigned char command, bool result)
{
    DefaultSend(command, result?RESULT_SUCCESS:RESULT_FAIL);
    m_funcCodeSend = 0xC1;
    m_dataSend = CamGainControl::GetInstance().GetGain();
//    SendFrame();
}

void ProtocolPod613::ReportFocusInc(bool result)
{
    ReportFocus(0x01,result);
}

void ProtocolPod613::ReportFocusDec(bool result)
{
    ReportFocus(0x02,result);
}

void ProtocolPod613::ReportFocusSet(bool result)
{
    ReportFocus(0x10,result);
}

void ProtocolPod613::ReportFocus(unsigned char command, bool result)
{
    DefaultSend(command, result?RESULT_SUCCESS:RESULT_FAIL);
    m_funcCodeSend = 0xC2;
    m_dataSend = ProtocolMCU::GetInstance().GetFocus();
//    SendFrame();
}

void ProtocolPod613::ReportContrast(unsigned char command, bool result)
{
    DefaultSend(command, result?RESULT_SUCCESS:RESULT_FAIL);
    m_funcCodeSend = 0xC3;
    m_dataSend = GetContrast();
//    SendFrame();
}

void ProtocolPod613::ReportOutMode(unsigned char command, bool result)
{
    DefaultSend(command, result?RESULT_SUCCESS:RESULT_FAIL);
    m_funcCodeSend = 0xC4;
//    auto mode = GetOutMode();

    if((command==0x01)||(command==0x02)||(command==0x03)||(command==0x10))
    {
        switch(GetOutMode())
        {
        case OutMode::ORIGINAL:
            m_dataSend = 0x01;
            break;
        case OutMode::ENHANCE:
            m_dataSend = 0x02;
            break;
        case OutMode::SUPERPOSITION:
            m_dataSend = 0x03;
            break;
        default:
            break;
        }
    }
    else
    {
        m_dataSend = command;
    }
//    SendFrame();
}

void ProtocolPod613::ReportWorkMode(unsigned char command, bool result)
{
    DefaultSend(command, result?RESULT_SUCCESS:RESULT_FAIL);
    m_funcCodeSend = 0xC5;
    m_dataSend = CamControl::GetInstance().GetWorkMode()?0x1:0x02;
//    SendFrame();
}

void ProtocolPod613::DefaultSend(unsigned char command, unsigned char result)
{
    m_commandSend = command;
    m_resultSend = result;
}

#endif //#ifndef POD_IS_205

/********************************************************************\
 *
 * REVISION RECORDS
 *
\*********************************************************************/
/*********************************************************************/
/*
 *
 *
 *
\*********************************************************************/
/*------------------------------The End------------------------------*/






